﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using PolePosition.v3.EntityLibrary;

namespace PolePosition.v3.Desktop.Grids
{
    public partial class GridXMissionInspection : GridBase
    {
        public GridXMissionInspection() 
            : base(Common.Enumerations.DataTypes.ProjectsXmissionInspection)
        {
            InitializeComponent();
            advTreeInspectionVisits.Visible = false;
            contextMenuBarGrid.SetContextMenuEx(advTreeInspectionVisits, buttonItemContextMenu);
        }

        private bool m_CombosBound;
        private void BindCombos()
        {
            if (Program.LookupTableData != null && !m_CombosBound)
            {
                bindingSourceKv.DataSource = Program.LookupTableData.VendorVisitType;
                comboBoxExKv.DisplayMember = VendorVisitTypeEnumerations.Columns.Name.ToString();
                comboBoxExKv.ValueMember = VendorVisitTypeEnumerations.Columns.Vendor_Visit_TypeId.ToString();
                comboBoxExKv.DataSource = bindingSourceKv;

                bindingSourcePoleSpecies.DataSource = Program.LookupTableData.XMissionStructuresPoleSpecies;
                comboBoxExPoleSpecies.DisplayMember = XMissionStructuresPoleSpeciesEnumerations.Columns.Name.ToString();
                comboBoxExPoleSpecies.ValueMember = XMissionStructuresPoleSpeciesEnumerations.Columns.XMissionStructures_PoleSpeciesId.ToString();
                comboBoxExPoleSpecies.DataSource = bindingSourcePoleSpecies;

                bindingSourcePoleClass.DataSource = Program.LookupTableData.XMissionStructuresPoleClasses;
                comboBoxExPoleClass.DisplayMember = XMissionStructuresPoleClassesEnumerations.Columns.Name.ToString();
                comboBoxExPoleClass.ValueMember = XMissionStructuresPoleClassesEnumerations.Columns.XMissionStructures_PoleClassId.ToString();
                comboBoxExPoleClass.DataSource = bindingSourcePoleClass;

                bindingSourcePoleTreatment.DataSource = Program.LookupTableData.XMissionStructuresPoleTreatments;
                comboBoxExPoleTreatment.DisplayMember = XMissionStructuresPoleTreatmentEnumerations.Columns.Name.ToString();
                comboBoxExPoleTreatment.ValueMember = XMissionStructuresPoleTreatmentEnumerations.Columns.XMissionStructures_PoleTreatmentId.ToString();
                comboBoxExPoleTreatment.DataSource = bindingSourcePoleTreatment;

                bindingSourcePoleTopType.DataSource = Program.LookupTableData.XMissionStructuresTopTypes;
                comboBoxExPoleTopType.DisplayMember = XMissionStructuresTopTypeEnumerations.Columns.Name.ToString();
                comboBoxExPoleTopType.ValueMember = XMissionStructuresTopTypeEnumerations.Columns.XMissionStructures_TopTypeId.ToString();
                comboBoxExPoleTopType.DataSource = bindingSourcePoleTopType;

                bindingSourceResultType.DataSource = Program.LookupTableData.VendorInspVisitXmissionStructuresResultTypes;
                comboBoxExResultType.DisplayMember = VendorInspVisitXmissionStructuresResultTypeEnumerations.Columns.Name.ToString();
                comboBoxExResultType.ValueMember = VendorInspVisitXmissionStructuresResultTypeEnumerations.Columns.Vendor_InspVisit_XmissionStructures_ResultTypeId.ToString();
                comboBoxExResultType.DataSource = bindingSourceResultType;

                bindingSourceInspectionType.DataSource = Program.LookupTableData.VendorInspVisitXmissionStructuresInspectionType;
                comboBoxExInspectionType.DisplayMember = VendorInspVisitXmissionStructuresInspectionTypeEnumerations.Columns.Name.ToString();
                comboBoxExInspectionType.ValueMember = VendorInspVisitXmissionStructuresInspectionTypeEnumerations.Columns.Vendor_InspVisit_XmissionStructures_Inspection_TypeId.ToString();
                comboBoxExInspectionType.DataSource = bindingSourceInspectionType;

                bindingSourceMap.DataSource = Program.LoginResult.Polygons;
                comboBoxExMap.DisplayMember = UtilityPolygonEnumerations.Columns.Name.ToString();
                comboBoxExMap.ValueMember = UtilityPolygonEnumerations.Columns.Utility_PolygonId.ToString();
                comboBoxExMap.DataSource = bindingSourceMap;

                bindingSourceElectricalProblem.DataSource = ComboOptions.YesNoForBoolean;
                comboBoxExElectricalProblem.DisplayMember = "Text";
                comboBoxExElectricalProblem.ValueMember = "Value";
                comboBoxExElectricalProblem.DataSource = bindingSourceElectricalProblem;

                m_CombosBound = true;
            }
        }
      
        #region Asynch Processing (Implements IGridBase)

        public override void DoAsynchLoad(DoWorkEventArgs e)
        {
            advTreeInspectionVisits.Invoke(new MethodInvoker(delegate { advTreeInspectionVisits.Enabled = false; }));

            int _TotalRecords;
            e.Result = ServiceProxy.UtilityService.GetXmissionInspectionProjectPoleData(DataKeyId, PageSize, PageNumber, CurrentSearchType, CurrentSearchText, out _TotalRecords);
            TotalItems = _TotalRecords;
        }

        public override void AsychLoadComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            base.AsychLoadComplete(sender, e);
            //do whatever needs to be done after asynch work complete (BackgroudWorker_RunWorkerCompleted)
            if (e.Cancelled)
            {

            }

            if (e.Error != null || e.Result == null)
            {
                //todo: create some sort of error dialog and use it here instead of messagebx
                DevComponents.DotNetBar.MessageBoxEx.Show("Error retrieving project data.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            var _data = e.Result as List<VendorInspVisitXmissionStructuresPoleEntity>;

            if (_data == null || _data.Count() == 0)
            {
                if (CurrentSearchType == Common.Enumerations.GridSearchByTpes.None)
                    DevComponents.DotNetBar.MessageBoxEx.Show("No data found for the selected transmission inspection project.", "No Data Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    DevComponents.DotNetBar.MessageBoxEx.Show("No data found for the transmission inspection project with the applied search.", "No Data Found", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }


            bindingSourceGridDataParent.DataSource = _data;
            advTreeInspectionVisits.DataSource = bindingSourceGridDataParent;
            advTreeInspectionVisits.Bind();
            advTreeInspectionVisits.Visible = true;
            advTreeInspectionVisits.Enabled = true;
        }

        public override void DoAsynchSave(DoWorkEventArgs e)
        {

        }

        public override void AsychSaveComplete(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        public override void DoAsynchGetKml(DoWorkEventArgs e)
        {
            advTreeInspectionVisits.Invoke(new MethodInvoker(delegate { advTreeInspectionVisits.Enabled = false; }));

            Kml = ServiceProxy.UtilityService.GetXmissionInspectionProjectStructureKML(
                DataKeyId, CurrentSearchType, CurrentSearchText);
        }

        public override void AsychGetKmlComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            base.AsychGetKmlComplete(sender, e);
            advTreeInspectionVisits.Enabled = true;

            if (e.Cancelled)
            {
                // not sure what to do
            }

            if (e.Error != null)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Error retrieving project data.",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ShowKmlInMap();
        }

        #endregion

        #region Context Menu / Main Bar (Implements IGridBase)

        public override void DoSearch(Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {

        }

        public override void DoEdit()
        {
            var _Visit = advTreeInspectionVisits.GetSelectedEntity() as VendorInspVisitXmissionStructuresPoleEntity;

            var _Editor = new Editors.EditXmissionInspectionVisit(true, _Visit);
            if (_Editor.ShowDialog() == DialogResult.OK)
            { 
                //update the record in the grid
            
            }
            _Editor.Dispose();
        }

        public override void DoOpen()
        {
            var _Visit = advTreeInspectionVisits.GetSelectedEntity() as VendorInspVisitXmissionStructuresPoleEntity;

            var _Editor = new Editors.EditXmissionInspectionVisit(false, _Visit);
            _Editor.ShowDialog();
            _Editor.Dispose();
        }
       
        public override void DoMapForGrid()
        {
            StartAsynchGetKml();
        }

        public override void DoMapForRecord()
        {
            StartAsynchGetKml();
        }

        public override bool ContextShowAllowed()
        {
            var _Pole = advTreeInspectionVisits.GetSelectedEntity() as VendorInspVisitXmissionStructuresPoleEntity;

            if (_Pole == null)
                return false;
        
            SelectedRowLatitude = _Pole.Latitude;
            SelectedRowLongitude = _Pole.Longitude;
            
            return true;
        }

        #endregion

        public void ShowData(int projectId, string projectName)
        {
            DataKeyId = projectId;
            BindCombos();
            DataDescription = projectName;
            advTreeInspectionVisits.Visible = false;
            DataKeyId = projectId;
            StartAsynchLoad();
        }

    }
}
